#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#

"""This module provides utility methods related to metadata."""


import sorting_column_sets
import digital_content_wizard_exceptions


def GenerateLevelMap(level_values, level):
  """Generates array of level values mapped to incremental string indexes.

  Args:
    level_values: Array of string values for a particular level.
    level: Number indicating the level.

  Returns:
    Array containing level values mapped to incremental string indexes.
  """
  level_map = []
  i = 1
  for value in level_values:
    name_value_map = {}
    name_value_map['name'] = 'level' + str(level) + '_' + str(i)
    name_value_map['value'] = value
    level_map.append(name_value_map)
    i += 1
  return level_map


def GetUniqueValueMaps():
  """Creates maps of unique values for all the page tree level nodes.

  Returns:
    (l1_map, l2_map, l3_map) Tuple containing map for unique values for level1,
    level2 and level3.

  Raises:
    GlobalMemoryLostException: An error occurred when a global instance
    maintaining unique values of sorting columns is lost.
  """
  set_manager = sorting_column_sets.SetManager()
  level1_values = set_manager.GetUniqueValues(1)
  level2_values = set_manager.GetUniqueValues(2)
  level3_values = set_manager.GetUniqueValues(3)
  if not level1_values or not level2_values:
    raise digital_content_wizard_exceptions.GlobalMemoryLostException(
        'Loss of Global Memory... Cannot proceed')
  return (GenerateLevelMap(level1_values, 1),
          GenerateLevelMap(level2_values, 2),
          GenerateLevelMap(level3_values, 3))
